package org.zwt.common.formatter.impl;

import org.zwt.common.entity.HttpResponse;
import org.zwt.common.formatter.IResponseFormatter;

public class SimpleResponseFormatter implements IResponseFormatter {
    @Override
    public HttpResponse parse(String response) {
        return null;
    }

    @Override
    public String format(HttpResponse response) {
        StringBuffer sb = new StringBuffer();
        /*
         * 1.开始行,版本,状态
         * */
        sb.append(response.getVersion())
                .append(" ")
                .append(response.getStatus())
                .append("\r\n");
        /*
        * 2.响应头
        * */

        response.getHeaderNames().forEach(k ->{
            sb.append(k)
                    .append(":")
                    .append(response.getHeader(k))
                    .append("\r\n");
        });
        sb.append("\r\n");
        /*
        * 3.响应体
        * */
        if (response.getBody() != null){
            sb.append(response.getBody());
        }
        return sb.toString();
    }
}
